#ifndef __INClogFileh
#define __INClogFileh

#include <atomic>
#include <mutex>
#include <sstream>
#include <loglvl.h>
#include <declarecClass.h>
#include <utils.h>

class LogFile
{
public:
    ~LogFile();
    LogFile(const char * file = nullptr, int fileCnt = 10,int fileSize = 10, int level = LOG_LVL_INFO);
    int getLogLevel();
    STATUS setLogLevel(int level);
    std::string getLogName();
    bool getLogTraceDebug();
    STATUS setLogTraceDebug(bool value);
    void hexdump(const char* name, uint8_t width, const char* buf, uint16_t size);
    template <typename... Args>
    size_t write(int level, const char* file, const char* func, int line, const char* format, Args... args)
    {
        COND_CHECK_RET_VAL((level & LOG_LVL_MASK) > m_logLevel, 0);
        std::ostringstream oss;
        formatDateTime(oss);
        formatFileInfo(oss, level, file, func, line);
        oss << utils::format(format, args...);
        return write(oss);
    }

private:
    size_t write(const std::string& outString);
    size_t write(const char* buf, size_t len);
    size_t write(std::ostringstream&);
    void file_rotate(void);
    bool file_retate_check(void);
    void reOpen();
    void close();
    void formatDateTime(std::ostringstream&);
    void formatFileInfo(std::ostringstream&, int, const char*, const char*, int);

private:
    DISABLE_COPY_CLASS(LogFile);
    std::atomic<bool> m_debug;
    int m_fileCnt;
    int m_fileSzie;
    int m_logLevel;
    std::string m_fileName;
    std::mutex m_lock;
    FILE *m_file;
};
#endif